using System.Windows;

namespace MermaidTextEditor
{
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // 处理命令行参数
            if (e.Args.Length > 0)
            {
                string firstArg = e.Args[0];

                // 处理文件关联注册命令（以管理员权限重启后执行）
                if (firstArg.StartsWith("/register:", StringComparison.OrdinalIgnoreCase))
                {
                    string extension = firstArg.Substring(10); // 去掉 "/register:" 前缀
                    var window = new MainWindow();
                    window.Show();
                    
                    // 延迟执行注册操作，确保窗口已加载
                    window.Dispatcher.InvokeAsync(() =>
                    {
                        window.ExecuteFileAssociationCommand("register", extension);
                    }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
                    return;
                }
                else if (firstArg.StartsWith("/unregister:", StringComparison.OrdinalIgnoreCase))
                {
                    string extension = firstArg.Substring(12); // 去掉 "/unregister:" 前缀
                    var window = new MainWindow();
                    window.Show();
                    
                    // 延迟执行取消注册操作，确保窗口已加载
                    window.Dispatcher.InvokeAsync(() =>
                    {
                        window.ExecuteFileAssociationCommand("unregister", extension);
                    }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
                    return;
                }
                // 普通文件打开
                else if (System.IO.File.Exists(firstArg))
                {
                    // 将文件路径传递给主窗口
                    var mainWindow = new MainWindow(firstArg);
                    mainWindow.Show();
                    return;
                }
            }

            // 正常启动
            var normalWindow = new MainWindow();
            normalWindow.Show();
        }
    }
}

